const { collectionsModel } = require('../models/collectionsModel');
// 增加收藏
module.exports.createCollections = async (req, res, next) => {
    const data = req.body;
    console.log(data);
    const findItem = await collectionsModel.find(data)
    if (findItem.length > 0) {
        res.send({
            message: '添加失败',
            code: 0
        })
    } else {
        const result = await collectionsModel.create(data);
        if (result != null) {
            res.send({
                message: '添加成功',
                code: 1
            });
        }
    }
};

// 取消收藏
module.exports.deleteCollections = async (req, res, next) => {
    const data = req.query;
    const result = await collectionsModel.deleteOne(data);
    if (result.deletedCount != 0) {
        res.send({
            message: '删除成功',
            code: 1
        });
    } else {
        res.send({
            message: '删除失败',
            code: 0
        })
    }
};

// 获取所有收藏题目
module.exports.getCollections = async (req, res, next) => {
    const data = req.query;
    const result = await collectionsModel.find({ studentId: data._id })
        .populate('studentId')
        .populate('exerciseId')
    if (result.length > 0) {
        res.send({
            code: 1,
            message: '获取成功',
            result
        });
    } else {
        res.send({
            code: 0,
            message: '获取失败'
        })
    }

};